package com.bingxue.edu.framework.common.util;

import com.bingxue.edu.framework.common.constant.HttpHeaders;
import com.bingxue.edu.framework.exception.SystemException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * http上下文工具类，用于获取servlet请求和响应
 */
@SuppressWarnings("unused")
public class HttpContextHolder {

    /**
     * 获取servlet request
     *
     * @return HttpServletRequest
     */
    public static HttpServletRequest getRequest() {
        var requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            return requestAttributes.getRequest();
        } else {
            throw new SystemException("非web请求获取HttpRequest失败");
        }
    }

    /**
     * 获取servlet response
     *
     * @return HttpServletResponse
     */
    public static HttpServletResponse getResponse() {
        var requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            return requestAttributes.getResponse();
        } else {
            throw new SystemException("非web请求获取HttpResponse失败");
        }
    }

    /**
     * 获取请求地址
     *
     * @return 请求地址
     */
    public static String getRequestUri() {
        try {
            HttpServletRequest request = getRequest();
            return request.getRequestURI();
        } catch (Exception exception) {
            return "-";
        }
    }

    /**
     * 获取客户端ip
     *
     * @return ip
     */
    public static String getClientAddress() {
        try {
            HttpServletRequest request = getRequest();
            String address = request.getHeader(HttpHeaders.X_FORWARDED_FOR);
            if (Strings.isNotBlank(address)) {
                return address;
            }
            address = request.getHeader(HttpHeaders.X_REAL_IP);
            if (Strings.isNotBlank(address)) {
                return address;
            }
            address = request.getHeader(HttpHeaders.PROXY_CLIENT_IP);
            if (Strings.isNotBlank(address)) {
                return address;
            }
            address = request.getRemoteAddr();
            if (Strings.isNotBlank(address)) {
                return address;
            }
            return "Unknown";
        } catch (Exception exception) {
            return "Unknown";
        }
    }

    /**
     * 获取请求头值
     */
    public static String getHeader(String name) {
        return getRequest().getHeader(name);
    }

    /**
     * 获取cookie值
     */
    public static String getCookie(String name) {
        Cookie[] cookies = getRequest().getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie != null && name.equals(cookie.getName())) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    /**
     * 在请求上下文里写入一个值
     *
     * @param name  名字
     * @param value 值
     */
    public static void addAttribute(String name, String value) {
        getRequest().setAttribute(name, value);
    }

    /**
     * 在响应头里写入一个值
     *
     * @param name  名字
     * @param value 值
     */
    public static void addHeader(String name, String value) {
        getResponse().setHeader(name, value);
    }

}
